FILE_LOCATION   =	"/ifs/scratch/c2b2/ip_lab/ddd2124"

setwd(FILE_LOCATION)


GC_FILE<-read.table("gparchild.lst",header=F,stringsAsFactors=F)


poplist<-read.table("PopIndex.txt",header=F, stringsAsFactors=F)
poplist<-poplist[,1]


#enact these once you have the lengths and numbers files
Lengths<-read.table("Lengths.txt",header=F)
Numbers<-read.table("Numbers.txt",header=F)


gcl<-integer(0)
gcn<-integer(0)

for (i in seq(length(GC_FILE[,1])))
{
	if ((A<-which(poplist==GC_FILE[i,1]))<(B<-which(poplist==GC_FILE[i,2])))
	{gcl<-c(gcl,Lengths[A,B])
	 gcn<-c(gcn,Numbers[A,B])}
	else 	{gcl<-c(gcl,Lengths[B,A])
		 gcn<-c(gcn,Numbers[B,A])}
}

gclcdf<-ecdf(gcl)
gcncdf<-ecdf(gcn)


g<-length(Lengths[,1])



#####################################################
##################GC MATRICES########################

GCNMAT <- Lengths
for (i in seq(g-1))
{
	GCNMAT[i,] <- gcncdf(Numbers[i,])
	GCNMAT[i,] <- apply(rbind(GCNMAT[i,],1-GCNMAT[i,]), 2, min)
}

GCLMAT <- PCNMAT
for (i in seq(g-1))
{
	GCLMAT[i,] <- gclcdf(Lengths[i,])
	GCLMAT[i,] <- apply(rbind(GCLMAT[i,],1-GCLMAT[i,]), 2, min)
}

GCNMAT2 <- GCNMAT+ t(GCNMAT)
write.table(GCNMAT2,"GCNMAT.txt",row.names=F,col.names=F,quote=F)
rm(GCNMAT2, GCNMAT)
gc()
GCLMAT2 <- GCLMAT+ t(GCLMAT)
write.table(GCLMAT2,"GCLMAT.txt",row.names=F,col.names=F,quote=F)
rm(GCLMAT2, GCLMAT)
gc()



###############################################################################
# add matrices for avg lengths

AverageLengths <- Numbers / (Lengths + .001)

gca <-integer(0)

g<-length(AverageLengths[,1])

for (i in seq(length(PC_FILE[,1])))
{
	if ((A<-which(poplist==GC_FILE[i,1]))<(B<-which(poplist==GC_FILE[i,2])))
	{gca<-c(gca,AverageLengths[A,B])}
	else 	{gca<-c(gca,AverageLengths[B,A])}
}

gcacdf<-ecdf(gca)

GCAMAT<- AverageLengths
for (i in seq(g-1))
{
	GCAMAT[i,] <- gcacdf(AverageLengths[i,])
	GCAMAT[i,] <- apply(rbind(GCAMAT[i,],1-GCAMAT[i,]), 2, min)
}

GCAMAT<- GCAMAT+ t(GCAMAT)
write.table(GCAMAT,"GCAMAT.txt",row.names=F,col.names=F,quote=F)
rm(GCAMAT, gca, gcacdf)
gc()
################
quit()